package iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.adapter;

import static android.content.Context.MODE_PRIVATE;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.R;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.activity.secondActivity.ActivityAddProductImg;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.entity.Product;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.IProductService;
import iet.jxufe.cn.android.productanti_counterfeitingtraceabilitysystem.service.serviceImpl.ProductService;

public class ShopAdapter extends ArrayAdapter<Product> {
    TextView productName;
    ImageView productImage;
    TextView productDetails;
    TextView but_buy;
    IProductService productService;

    public ShopAdapter(@NonNull Context context, @NonNull List<Product> products, @NonNull IProductService productService) {
        super(context, 0, products);
        this.productService = productService;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_shop, parent, false);
        }

        Product product = getItem(position);

        // 获取视图中的控件
        productName = convertView.findViewById(R.id.product_name);
        productImage = convertView.findViewById(R.id.product_image);
        productDetails = convertView.findViewById(R.id.product_details);
        but_buy = convertView.findViewById(R.id.btn_buy);

        // 设置产品名称和生产日期
        productName.setText(product.getProductName());

        // 加载产品图片
        if (product.getImage() != null) {
            Bitmap bitmap = BitmapFactory.decodeByteArray(product.getImage(), 0, product.getImage().length);
            productImage.setImageBitmap(bitmap);
        } else {
            productImage.setImageResource(R.drawable.default_image); // 请替换为您默认的图片资源
        }

        // 设置产品详细信息
        String details = String.format("ID: %s\n分类: %s\n生产日期：%s\n价格: %.2f\n",
                product.getProductId(),
                product.getCategory(),
                product.getProduceDate(),
                product.getPrice());

        // 为产品详情设置点击事件，弹出对话框
        productDetails.setOnClickListener(v -> showProductDetailsDialog(product, details));

        // 设置修改超链接的点击事件
        but_buy.setOnClickListener(v -> {

            // Todo 购买成功向数据库反馈
            Boolean info =  productService.purchaseByConsumer(product.getProductId());
            if (info){
                Toast.makeText(getContext(),"成功购买",Toast.LENGTH_SHORT).show();
                remove(product); // ArrayAdapter 自带的 remove() 方法
                notifyDataSetChanged(); // 通知适配器刷新界面
                Log.d("Test","购买"+productService.purchaseByConsumer(product.getProductId()));
            } else Toast.makeText(getContext(),"购买时发生错误",Toast.LENGTH_SHORT).show();

        });

        return convertView;
    }

    // 展示商品详细信息
    private void showProductDetailsDialog(Product product, String details) {
        // 创建并显示包含产品详细信息的对话框
        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setTitle("产品详细信息");
        builder.setMessage(details);
        builder.setPositiveButton("确定", null);
        builder.show();
    }
}